package com.woniu.user.service.impl;

import com.woniu.common.pojo.dto.user.IncomeDto;
import com.woniu.common.pojo.vo.UserLoginVo;
import com.woniu.common.util.BCrypt;
import com.woniu.common.util.JwtUtils;
import com.woniu.user.entity.BalanceRecords;
import com.woniu.user.entity.UserInfo;
import com.woniu.user.mapper.BalanceRecordsMapper;
import com.woniu.user.mapper.UserInfoMapper;
import com.woniu.user.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 贾玉亮
 * @since 2023-01-02
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Resource
    private UserInfoMapper mapper;
    @Resource
    private BalanceRecordsMapper balanceRecordsMapper;
    @Override
    public String login(String userMobile) {
        return mapper.login(userMobile);
    }

    @Override
    @Cacheable(cacheNames = "user:info")
    public UserLoginVo getByMobile(String mobile) {
        UserInfo userInfo = mapper.getByMobile(mobile);
        userInfo.setUserMobile(mobile);
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setId(userInfo.getId());
        userLoginVo.setMobile(userInfo.getUserMobile());
        userLoginVo.setAvatar(userInfo.getAvatar());
        return userLoginVo;
    }

    @Override
    public String getPayPasswordByUserId(String userId) {
        return mapper.getPayPasswordByUserId(userId);
    }

    @Override
    public UserInfo getUserByMobile(String mobile) {
        return mapper.getUserByMobile(mobile);
    }

    @Override
    @Transactional
    public void incomeByUser(IncomeDto dto) {
        BigDecimal money = dto.getMoney();
        UserInfo userInfo = mapper.selectById(dto.getFk_user_id());
        BigDecimal balance = userInfo.getBalance();
        BigDecimal add = balance.add(money);
        userInfo.setBalance(add);
        BalanceRecords balanceRecords = new BalanceRecords();
        balanceRecords.setFkUserId(userInfo.getId());
        balanceRecords.setMoney(money);
        //收益
        balanceRecords.setOperate("3");
        //余额记录表插入
        balanceRecordsMapper.insert(balanceRecords);
        //用户余额修改
        mapper.updateById(userInfo);
    }

    @Override
    public int registerUser(UserInfo userInfo) {

        String gensalt = BCrypt.gensalt(10, new SecureRandom());
        String loginPassword = BCrypt.hashpw(userInfo.getLoginPassword(), gensalt);
        String payPassword = BCrypt.hashpw(userInfo.getPayPassword(), gensalt);
        userInfo.setLoginPassword(loginPassword);
        userInfo.setPayPassword(payPassword);
        int insert = mapper.insert(userInfo);
        return insert;
    }
}
